#include "kernel_operator.h"
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;    
template<typename TYPE_X, typename TYPE_Y> class KernelAsinh {
    using T = TYPE_X;
public:
    __aicore__ inline KernelAsinh() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y,
                                uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        xGm.SetGlobalBuffer((__gm__ DTYPE_X*)x, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);

        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));

        pipe.InitBuffer(tmpBuffer, this->tileDataNum * sizeof(DTYPE_X));

        

        if constexpr (std::is_same_v<T, half>)
        {
            
            pipe.InitBuffer(tmp1, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(tmp2, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(tmp3, this->tileDataNum * sizeof(float));
        }


    }
    __aicore__ inline void Process() {

        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();
        DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();

        LocalTensor<DTYPE_X> tmp = tmpBuffer.Get<DTYPE_X>();

        
        if constexpr (std::is_same_v<DTYPE_X, half>){


            auto p1 = tmp1.Get<float>();
            auto p2 = tmp2.Get<float>();
            auto p3 = tmp3.Get<float>();

            //auto p3 =

            Cast(p1, xLocal, RoundMode::CAST_NONE, this->processDataNum);

            Cast(p2, xLocal, RoundMode::CAST_NONE, this->processDataNum);

            Cast(p3, xLocal, RoundMode::CAST_NONE, this->processDataNum);
            
            Maxs(p2,p1,static_cast<float>(0),this->processDataNum);

            Mul(p3,p2,p2,this->processDataNum);
            Adds(p3,p3,static_cast<float>(1.0),this->processDataNum);

            Sqrt(p3,p3,this->processDataNum);

            Add(p2,p2,p3,this->processDataNum);

            Ln(p2,p2,this->processDataNum);



            Mins(p3,p1,static_cast<float>(0),this->processDataNum);

            Mul(p1,p3,p3,this->processDataNum);

            Adds(p1,p1,static_cast<float>(1.0),this->processDataNum);

            Sqrt(p1,p1,this->processDataNum);

            Sub(p1,p1,p3,this->processDataNum);

            Ln(p1,p1,this->processDataNum);

            Muls(p1,p1,static_cast<float>(-1.0),this->processDataNum);

            Add(p1,p2,p1,this->processDataNum);

            Cast(yLocal, p1, RoundMode::CAST_NONE, this->processDataNum);   





        }

        else{


        

        Maxs(tmp,xLocal,static_cast<DTYPE_X>(0),this->processDataNum);

        Mul(yLocal,tmp,tmp,this->processDataNum);
        Adds(yLocal,yLocal,static_cast<DTYPE_X>(1.0),this->processDataNum);

        Sqrt(yLocal,yLocal,this->processDataNum);

        Add(tmp,yLocal,tmp,this->processDataNum);

        Ln(tmp,tmp,this->processDataNum);


        Mins(xLocal,xLocal,static_cast<DTYPE_X>(0),this->processDataNum);

        Mul(yLocal,xLocal,xLocal,this->processDataNum);

        Adds(yLocal,yLocal,static_cast<DTYPE_X>(1.0),this->processDataNum);

        Sqrt(yLocal,yLocal,this->processDataNum);

        Sub(yLocal,yLocal,xLocal,this->processDataNum);

        Ln(yLocal,yLocal,this->processDataNum);

        Muls(yLocal,yLocal,static_cast<DTYPE_X>(-1.0),this->processDataNum);

        Add(yLocal,yLocal,tmp,this->processDataNum);

}







        //处理数据量   this->processDataNum


        outQueueY.EnQue<TYPE_Y>(yLocal);
        inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe pipe;

    TBuf<QuePosition::VECCALC> tmpBuffer,tmp1,tmp2,tmp3;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;

    GlobalTensor<DTYPE_X> xGm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};
extern "C" __global__ __aicore__ void asinh(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    
    KernelAsinh<DTYPE_X, DTYPE_Y> op;
    op.Init(x, y, 
            tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum);  
    op.Process();
}